﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.UICommon;

namespace ESOL.WinUI.Sales
{
    public partial class frmMaintainDeliveryFeedback : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public SalesDeliveryFeedbackEntity SelectedFeedback { get; set; }
        #endregion

        #region Constructor
        public frmMaintainDeliveryFeedback(SalesDeliveryFeedbackEntity entity)
        {
            InitializeComponent();
            SelectedFeedback = entity;
            if (entity != null)
            {
                SalesDeliveryOrderEntity delivery = SalesDeliveryOrderBs.GetDeliveryOrderByID(entity.DeliveryOrderId);
                if (delivery != null)
                {
                    SetFieldsByDeliveryOrder(delivery);
                }
            }
            if (SelectedFeedback == null)
            {
                SelectedFeedback = new SalesDeliveryFeedbackEntity();
                SelectedFeedback.FeedbackId = Guid.NewGuid().ToString();
            }
        }
        public frmMaintainDeliveryFeedback(SalesDeliveryOrderEntity entity)
        {
            InitializeComponent();
            if (entity != null)
            {
                SetFieldsByDeliveryOrder(entity);
            }
            if (SelectedFeedback == null)
            {
                SelectedFeedback = new SalesDeliveryFeedbackEntity();
                SelectedFeedback.FeedbackId = Guid.NewGuid().ToString();
            }
        }
        #endregion

        #region Functions
        private void SetFieldsByCustomer(MstCustomerEntity customer)
        {
            if (customer != null)
            {
                txtCustomer.Tag = customer;
                txtCustomer.Text = customer.CustomerName;
                UICommonFunctions.BindContactCombobox(cmbCustomerContact, customer.CustomerId, false);
            }
            else
            {
                txtCustomer.Tag = null;
                txtCustomer.Text = "";
                UICommonFunctions.BindContactCombobox(cmbCustomerContact, "", false);
            }
        }
        private void SetFieldsByDeliveryOrder(SalesDeliveryOrderEntity entity)
        {
            if (entity == null)
            {
                txtDeliveryOrder.Tag = null;
                txtDeliveryOrder.Text = "";
                txtDeliveryOrderPerson.Text = "";
                SetFieldsByCustomer(null);
                return;
            }
            txtDeliveryOrder.Tag = entity;
            txtDeliveryOrder.Text = entity.DeliveryOrderCode;
            MstPersonEntity person = MstPersonBs.GetMstPersonByID(entity.DeliveryPerson);
            if (person != null)
            {
                txtDeliveryOrderPerson.Text = person.NameCn;
            }
            MstCustomerEntity customer = MstCustomerBs.GetMstCustomerByID(entity.CustomerId);
            SetFieldsByCustomer(customer);
        }
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbDeliveryType, "DW", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbFeeType, "FT", true);
        }
        private void SetFields()
        {
            if (SelectedFeedback == null)
            {
                return;
            }
            SalesDeliveryOrderEntity entity = SalesDeliveryOrderBs.GetDeliveryOrderByID(SelectedFeedback.DeliveryOrderId);
            if (entity != null)
            {
                SetFieldsByDeliveryOrder(entity);
            }
            txtFeedbackCode.Text = SelectedFeedback.FeedbackCode;
            if(string.IsNullOrEmpty(txtFeedbackCode.Text))
            {
                txtFeedbackCode.Text = "自动生成";
            }
            txtDeliveryPerson.Text = SelectedFeedback.DeliveryPerson;
            dtpDeliveryDate.EditValue = SelectedFeedback.DeliveryDate;
            txtDeliveryPerson.Text = SelectedFeedback.DeliveryPerson;
            txtReceivedPerson.Text = SelectedFeedback.ReceivedPerson;
            dtpReceivedDate.EditValue = SelectedFeedback.ReceivedDate;
            if (ThisActionType == ActionType.New && entity != null)
            {
                cmbDeliveryType.SetSelectedValue(entity.DeliveryType);
            }
            else
            {
                cmbDeliveryType.SetSelectedValue(SelectedFeedback.DeliveryType);
            }
            txtPostNo.Text = SelectedFeedback.PostNo;
            txtDeliveryAmount.Text = SelectedFeedback.DeliveryAmount.ToString("#0.00");
            cmbFeeType.SetSelectedValue(SelectedFeedback.FeeType);
            cmbStatus.SetSelectedValue(SelectedFeedback.Status);

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, SelectedFeedback.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, SelectedFeedback.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, SelectedFeedback.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, SelectedFeedback.LastModifiedBy);
        }

        private void SetEntity()
        {
            if (txtDeliveryOrder.Tag != null && txtDeliveryOrder.Tag.GetType() == typeof(SalesDeliveryOrderEntity))
            {
                SelectedFeedback.DeliveryOrderId = ((SalesDeliveryOrderEntity)txtDeliveryOrder.Tag).DeliveryOrderId;
            }
            SelectedFeedback.DeliveryPerson = txtDeliveryPerson.Text;
            if (dtpDeliveryDate.EditValue!=null)
            {
                SelectedFeedback.DeliveryDate = Convert.ToDateTime(dtpDeliveryDate.EditValue);
            }
            SelectedFeedback.DeliveryPerson = txtDeliveryPerson.Text;
            SelectedFeedback.ReceivedPerson = txtReceivedPerson.Text;
            if (dtpReceivedDate.EditValue != null)
            {
                SelectedFeedback.ReceivedDate = Convert.ToDateTime(dtpReceivedDate.EditValue);
            }
            if (cmbDeliveryType.SelectedValue != null && cmbDeliveryType.SelectedValue.ToString().Trim() != "")
            {
                SelectedFeedback.DeliveryType = cmbDeliveryType.SelectedValue.ToString();
            }
            SelectedFeedback.PostNo = txtPostNo.Text;
            SelectedFeedback.DeliveryAmount = CommonFunctions.ToDecimal(txtDeliveryAmount.Text);
            if (cmbFeeType.SelectedValue != null && cmbFeeType.SelectedValue.ToString().Trim() != "")
            {
                SelectedFeedback.FeeType = cmbFeeType.SelectedValue.ToString();
            }
            SelectedFeedback.Status = NormalStatus.STATUS03.ToString();

            if (ThisActionType == ActionType.New)
            {
                SelectedFeedback.CreatedDate = DateTime.Now;
                SelectedFeedback.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                SelectedFeedback.LastModifiedDate = DateTime.Now;
                SelectedFeedback.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = SalesDeliveryFeedbackBs.SaveFeedback(true, SelectedFeedback);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = SalesDeliveryFeedbackBs.SaveFeedback(false, SelectedFeedback);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private bool CheckData()
        {
            if (dtpDeliveryDate.EditValue == null)
            {
                MessageBox.Show("发货日期不能为空", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbDeliveryType.Text) || cmbDeliveryType.SelectedValue == null)
            {
                MessageBox.Show("发货方式不能为空", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpReceivedDate.EditValue == null)
            {
                MessageBox.Show("收货日期不能为空", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtReceivedPerson.Text))
            {
                MessageBox.Show("收货人不能为空", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtDeliveryAmount.Text))
            {
                MessageBox.Show("产生的费用不能为空", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbFeeType.Text) || cmbFeeType.SelectedValue == null)
            {
                MessageBox.Show("费用类别不能为空", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        #endregion

        #region Event
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            SetFields();
            base.OnShown(e);
        }
        #endregion
    }
}
